package example.telosys.persistence;

import java.util.List;

import org.demo.vo.bean.Country;
import org.demo.vo.bean.Customer;
import org.telosys.persistence.PersistenceManager;
import org.telosys.persistence.PersistenceManagerFactory;
import org.telosys.persistence.Query;
import org.telosys.persistence.TelosysPersistence;

import static org.telosys.persistence.PersistenceFunctions.*;

public class CountryService {

	protected PersistenceManager pm ;

	public CountryService(PersistenceManager pm) {
		super();
		this.pm = pm;
	}
	
	public Country insertCountry (String code, String name) 
	{
		Country country = new Country();
		country.setCode(code);
		country.setName(name);
		pm.insert(country);
		return country ;
	}
	
	public Country findCountry (String code) 
	{
		Country country = new Country();
		country.setCode(code);
		if ( pm.load(country) ) 
			return country ;
		else 
			return null ;
	}
	
	public boolean removeCountry (String code) 
	{
		Country country = new Country();
		country.setCode(code);
		return pm.delete(country);
	}
	
	public boolean updateCountryName (String code, String name) 
	{
		Country country = new Country();
		country.setCode(code);
		country.setName(name);
		return pm.update(country);
	}
	
	public List<Country> findAllCountries() 
	{
		return pm.loadAll(Country.class);
	}

	public List<Customer> findCustomers(Country country) 
	{
		Query<Customer> query = pm.createQuery(Customer.class, 
				Customer.$countryCode + " = ?", 
				//"order by " + Customer.$firstName + " ASC");
				orderBy(Customer.$firstName, Customer.$lastName) );
		
		System.out.println("QUERY SELECT = " + query.getSqlSelect() );
		int n = query.getParamCount();
		
		System.out.println("All params initialized ? " + query.allParametersInitialized() );
		
		query.setParameter(1, country.getCode() );

		System.out.println("All params initialized ? " + query.allParametersInitialized() );
		
		return pm.loadByQuery( query );
		
	/**
		//--- Get the owning side link
		// 1) get link
		link = getLink(country, "listOfCustomer");
		
		if ( mappedBy ) // Not owning side
		{
			link = getLink(Customer, mappedBy );
		}
		
		//--- Build the query
		Query<Customer> query = link.createQuery();
		
		//--- Set the query parameters ( the PK fields )
		query.setParameter(1, country.getCode() );
	**/
	}

	public int countAllCountries() 
	{
		return pm.countAll(Country.class);
	}


	public List<Country> findCountriesByName (String namePattern) 
	{
		Query<Country> query = pm.createQuery(Country.class, "name like ?" );
		query.setParameter(1, namePattern);
		
		return pm.loadByQuery( query );
	}
	
	public static void main( String args[] ) 
	{
		boolean b ;
		PersistenceManagerFactory pmf = TelosysPersistence.getPersistenceManagerFactory();
		PersistenceManager pm = pmf.createPersistenceManager();
		CountryService service = new CountryService(pm);
		
		System.out.println("pm.isOpen() : " + pm.isOpen() );
		
		System.out.println("pm.isAutoCommitMode() : " + pm.isAutoCommitMode() );
		
		b = service.removeCountry("ZZ");
		System.out.println("remove country : " + b );
		
		Country country = service.insertCountry("ZZ", "ZZ land");
		System.out.println("insert country : " + country );
		
		Country country2 = service.findCountry("ZZ");
		System.out.println("country found: " + country2 );
		
		System.out.println("count all : " + service.countAllCountries() );
		List<Country> list ;
		
		System.out.println("all countries : " );
		list = service.findAllCountries();
		for ( Country c : list ) {
			System.out.println(" . " + c );
		}
		
		String namePattern = "F%" ;
		System.out.println("countries for name '" + namePattern + "' :" );
		list = service.findCountriesByName(namePattern);
		for ( Country c : list ) {
			System.out.println(" . " + c );
		}
		
		loadCountryWithCustomers( service, "FR"  ) ;
		
		pm.close();
		System.out.println("pm.isOpen() : " + pm.isOpen() );
		
	}

	public static void loadCountryWithCustomers( CountryService service, String key  ) 
	{
		System.out.println("Try to load country '" + key + "'");
		Country country = service.findCountry(key);
		if ( country != null ) {
			System.out.println("Country found : " + country );
		}
		else {
			System.out.println("Country not found." );
			return;
		}
		
		System.out.println("Customers from " + country.getName() + " : ");
		
		List<Customer> customers = service.findCustomers(country);
		for ( Customer c : customers ) {
			System.out.println(" . " + c );
		}
		
	}
}
